package cn.jzq.spring.springioc.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author jzq
 * @date 2019/3/20 13:33
 */
@RequestMapping("/user")
@RestController
@CrossOrigin
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/{id}")
    public User getUser(@PathVariable("id") Integer id) {
        return userService.getUserById(id);
    }

    @PostMapping("")
    public String user(@RequestBody User user) {
        userService.saveUser(user);
        return "success";
    }

    @DeleteMapping("/{id}")
    public String user(@PathVariable("id") Integer id) {
        userService.deleteUser(id);
        return "success";
    }

    @GetMapping("")
    public Set<User> listUser() {
        return userService.listUser();
    }
}
